/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.bean;

import com.max.oti.system.bean.sppp.PracticeDetailBean;
import com.max.oti.system.model.Practice;
import com.max.oti.system.model.User;
import com.max.oti.system.service.interfac.IPracticeService;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.servlet.ServletException;
import org.omnifaces.util.Faces;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSeparator;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuModel;

/**
 *
 * @author max
 */
@ManagedBean
@SessionScoped
public class IndexLoginBean implements Serializable {
        
    private String navigation = "./content/home.xhtml";
    private User user = (User)Faces.getSession().getAttribute("usuario");
    private MenuModel menuModel;
    @ManagedProperty("#{practiceService}")
    IPracticeService practiceService;
    @ManagedProperty("#{practiceDetailBean}")
    PracticeDetailBean practiceDetailBean;
    Practice practice;
    
    public void goIndex() {
        navigation = "./content/home.xhtml";
    }
    
    public void generateMenuComission() {
        user = (User)Faces.getSession().getAttribute("usuario");
        menuModel = new DefaultMenuModel();
        
        /////////////////////////////////////////////////////
        DefaultMenuItem dmi = new DefaultMenuItem();
        dmi.setValue("Inicio");
        dmi.setUpdate(":panelCenter");
        dmi.setIcon("ui-icon-home");
        dmi.setOnstart("PF('blockUI').block();");
        dmi.setOncomplete("PF('blockUI').unblock();");
        dmi.setCommand("#{indexLoginBean.goIndex()}");
        
        menuModel.addElement(dmi);
        //////////////////////////////////////////////////////
        menuModel.addElement(new DefaultSeparator());
        //////////////////////////////////////////////////////
        DefaultSubMenu subMenu = new DefaultSubMenu();
        subMenu.setLabel("Practicante");
        subMenu.setIcon("ui-icon-person");
        
            dmi = new DefaultMenuItem();
            dmi.setValue("Nuevo");
            dmi.setUpdate(":panelCenter");
            dmi.setIcon("ui-icon-person");
            dmi.setOnstart("PF('blockUI').block();");
            dmi.setOncomplete("PF('blockUI').unblock();");
            dmi.setCommand("#{newPracticeBean.initBean()}");
            dmi.setAjax(false);
            dmi.setPartialSubmit(true);
            
        subMenu.getElements().add(dmi);
        
            dmi = new DefaultMenuItem();
            dmi.setValue("Eventos");
            dmi.setUpdate(":panelCenter");
            dmi.setIcon("ui-icon-check");
            dmi.setOnstart("PF('blockUI').block();");
            dmi.setOncomplete("PF('blockUI').unblock();");
            dmi.setCommand("#{practiceBean.initCollection()}");
            dmi.setIncludeViewParams(true);
            
        subMenu.getElements().add(dmi);
        
        menuModel.addElement(subMenu);
        ////////////////////////////////////////////////
        dmi = new DefaultMenuItem();
        dmi.setValue("Biblioteca");
        dmi.setUpdate(":panelCenter");
        dmi.setIcon("ui-icon-note");
        dmi.setOnstart("PF('blockUI').block();");
        dmi.setOncomplete("PF('blockUI').unblock();");
        dmi.setCommand("#{libraryBean.initBean()}");  
        
        menuModel.addElement(dmi);
        //////////////////////////////////////////
               
        
    }
    
    public void generateMenuPractice() {
        
        user = (User)Faces.getSession().getAttribute("usuario");
        
        try {
            practice = practiceService.getById(user.getId());
        } catch (Exception ex) {
            Logger.getLogger(IndexLoginBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        menuModel = new DefaultMenuModel();
        
        /////////////////////////////////////////////////////
        DefaultMenuItem dmi = new DefaultMenuItem();
        dmi.setValue("Inicio");
        dmi.setUpdate(":panelCenter");
        dmi.setIcon("ui-icon-home");
        dmi.setOnstart("PF('blockUI').block();");
        dmi.setOncomplete("PF('blockUI').unblock();");
        dmi.setCommand("#{indexLoginBean.goIndex()}");
        
        menuModel.addElement(dmi);
        //////////////////////////////////////////////////////
        menuModel.addElement(new DefaultSeparator());
        /////////////////////////////////////////////////////
        dmi = new DefaultMenuItem();
        dmi.setValue("Detalle");
        dmi.setUpdate(":panelCenter");
        dmi.setIcon("ui-icon-person");
        dmi.setOnstart("PF('blockUI').block();");
        dmi.setOncomplete("PF('blockUI').unblock();");
        dmi.setCommand("#{indexLoginBean.goDetailPractice()}");
        
        menuModel.addElement(dmi);
        //////////////////////////////////////////////////////
        
        dmi = new DefaultMenuItem();
        dmi.setValue("Biblioteca");
        dmi.setUpdate(":panelCenter");
        dmi.setIcon("ui-icon-note");
        dmi.setOnstart("PF('blockUI').block();");
        dmi.setOncomplete("PF('blockUI').unblock();");
        dmi.setCommand("#{libraryBean.initBean()}");  
        dmi.setParam("biblioteca", "./content/library_practice.xhtml");
        
        menuModel.addElement(dmi);
    }
    
    public void goDetailPractice() {
        navigation = "./content/detail_practice.xhtml";
        practiceDetailBean.setPractice(practice);
    }
    
    public void logOut() throws ServletException, IOException {
        Faces.removeSessionAttribute("usuario");
        Faces.logout();
        Faces.invalidateSession();
        System.out.println("Cerrando Sesión");
        Faces.redirect("login.jsf");
    }

    public String getNavigation() {
        System.out.println("Contenido: " + navigation);
        return navigation;
    }

    public void setNavigation(String navigation) {
        this.navigation = navigation;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setPracticeService(IPracticeService practiceService) {
        this.practiceService = practiceService;
    }

    public MenuModel getMenuModel() {
        return menuModel;
    }

    public void setMenuModel(MenuModel menuModel) {
        this.menuModel = menuModel;
    }

    public PracticeDetailBean getPracticeDetailBean() {
        return practiceDetailBean;
    }

    public void setPracticeDetailBean(PracticeDetailBean practiceDetailBean) {
        this.practiceDetailBean = practiceDetailBean;
    }

    public Practice getPractice() {
        return practice;
    }

    public void setPractice(Practice practice) {
        this.practice = practice;
    }
    
}
